import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { FileTableForm, FileEditForm, FolderFileInfo } from "./types";

/**
 * 上传文件
 *
 * @param file
 */
export function getFiles(
  folderId?: number | null,
  courseId?: number
): AxiosPromise<FolderFileInfo> {
  return request({
    url: "/api/v1/course/folder/files",
    method: "get",
    params: {
      folderId,
      courseId,
    },
  });
}

/**
 * 上传文件
 *
 * @param file
 */
export function getStudyFiles(
  folderId: number | null,
  courseId?: number
): AxiosPromise<FolderFileInfo> {
  return request({
    url: "/api/v1/course/folder/files",
    method: "get",
    params: {
      folderId,
      courseId,
    },
  });
}

/**
 *
 * @param filePath 文件完整路径
 */
export function saveFile(data: FileEditForm, courseId: number) {
  return request({
    url: "/api/v1/course/folder/save",
    method: "post",
    data,
    params: {
      courseId,
    },
  });
}

/**
 *
 * @param filePath 文件完整路径
 */
export function deleteCourseFile(id: number, type: number) {
  return request({
    url: "/api/v1/course/folder/delete",
    method: "delete",
    params: {
      id,
      type,
    },
  });
}

/**
 *
 * @param filePath 文件完整路径
 */
export function deleteCourseFiles(data: FileTableForm[]) {
  return request({
    url: "/api/v1/course/folder/deleteList",
    method: "post",
    data,
  });
}

/**
 *
 * @param filePath 文件完整路径
 */
export function uploadCourseAnnex(
  file: File,
  md5: string,
  folderId: number | null,
  courseId: number
) {
  const formData = new FormData();
  formData.append("file", file);
  return request({
    url: "/api/v1/course/folder/addFile",
    method: "post",
    headers: {
      "Content-Type": "multipart/form-data",
    },
    data: formData,
    params: {
      md5,
      folderId,
      courseId,
    },
  });
}
